#include <OsgClassDataSource/FileNodeOsg.h>
#include <iostream>
#include <osgDB/ReadFile>
//#include <EngineFunctions.h>
#include <FunctionsPluginOsg.h>
#include <QFileInfo>
#include <ObjectInspectorDirectorOsg.h>
//#include <QFileDialog>
#include <EDesignerCore.h>
#include <FileNode.h>

FileNodeOsg::FileNodeOsg( QObject *parent /*= 0*/ ) : MatrixTransformOsg(parent)
{
    //QString file_name = QFileDialog::getOpenFileName(0, "Load File","","*.*");



    m_object = new FileNode("");


    m_url = QUrl(QString(""));

    //m_object = m_fileNode;
    this->refreshInfoContainer();
}

FileNodeOsg::FileNodeOsg(QString str)
{
    QString file_name = str;


    m_object = new FileNode(file_name.toStdString());

    m_url = QUrl(file_name);
    //m_object = m_fileNode;
    this->refreshInfoContainer();

}

FileNodeOsg::~FileNodeOsg()
{

}

QUrl FileNodeOsg::getFileUrl()
{
    FileNode* fileNode = dynamic_cast<FileNode*>(m_object.get());
    if(fileNode != NULL)
    {
        m_url = QUrl(QString::fromStdString( fileNode->getFileName()) );

    }

    return m_url;
}

void FileNodeOsg::setFileUrl(QUrl url)
{
    std::cout<<"FileNodeOsg::setFileUrl"<<url.toString().toStdString()<<std::endl;
    if(url == m_url)
    {
        std::cout<<"FileNodeOsg::setFileUrl url is same"<<std::endl;
        return;
    }

    m_url = url;
    //EngineFunctions::enginePause();
    FunctionsPluginOsg::EDesignerCoreSimulationWaitUpdateEndAndStopSim();

    FileNode* fileNode = dynamic_cast<FileNode*>(m_object.get());

    //delet all the child item
    ObjectInspectorItem* item = getObjItem();
    item->removeRows(0, item->rowCount());

    //change the fileNode
    //std::cout<<"fileNode = "<<typeid(*m_object.get()).name()<<std::endl;
    if(fileNode == NULL)return;
    fileNode->changeFilePath(m_url.toString().toStdString());

    //save model file to project directory
    //QString newFileName = saveModeFileToProjectDir(m_url.toString());
    //m_url = QUrl(newFileName);

    FunctionsPluginOsg::EDesignerCoreSimulationStopWaitUpdateEnd();
}

void FileNodeOsg::copyFrom(EObject *objPtr)
{
    MatrixTransformOsg::copyFrom(objPtr);
    FileNodeOsg* objOsgPtr = dynamic_cast<FileNodeOsg*>(objPtr);
    if(objOsgPtr == NULL)return;
    this->m_url = objOsgPtr->m_url;

}

EObject *FileNodeOsg::clone()
{
    FileNodeOsg* newObj = new FileNodeOsg();
    newObj->copyFrom(this);
    return newObj;
}

bool FileNodeOsg::isTraverseChildEObject()
{
    return false;
}

QString FileNodeOsg::saveModeFileToProjectDir(const QString &fileName)
{
    QFileInfo fileInfo(fileName);

    QString baseName = fileInfo.baseName();

    QString proDir = EDesignerCore::instance()->getCurrentProjectDir();
    QString newfileName = proDir + QString("/modelData/") + baseName + QString(".ive");
    FileNode* fileNode = dynamic_cast<FileNode*>(m_object.get());

    if(fileNode == NULL)
    {
        std::cout<<"Error FileNodeOsg::saveModeFileToProjectDir fileNode = NULL"<<std::endl;
    }
    else
    {
        fileNode->saveToFile(newfileName.toStdString());
    }
    return newfileName;
}

